<?php
			
	/* SVN FILE: $Id: controllo.php 26 2011-03-31 13:07:58Z michele.andreoletti@gmail.com $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: michele.andreoletti@gmail.com $
	 * @version $Revision: 26 $
	 * @lastrevision $Date: 2011-03-31 13:07:58 +0000 (Thu, 31 Mar 2011) $
	 * @filesource $URL: http://arcadia.googlecode.com/svn/trunk/fornitori/controllo.php $
	 */

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';

	require_once('../include/view.php');
	
	if (!empty($iView)) {
		$sHeaderTitle = $sAPP_NAME." &raquo; ".$aData['sFornitore']." &raquo; ".$aData['sDescrizione'];
		$sTableTitle = "<div style='font-size: 0.7em'>".$aData['sFornitore']."</div>";
		$sTableTitle .= "<div><a href='./view.php?iView=".$aData['iContratto']."'>".$aData['sDescrizione']."</a></div>";
	} else  {
		$sHeaderTitle = $sAPP_NAME." &raquo; $sTableTitle";
	}
	
	$html .= "<div id='tabs'><ul>$sTabsHtml</ul>";
	
	$i = 5;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i))) {
		
		$html .= "<div id='tabs-$i'>";
			
			$bShowSollecito = false;
			$bShowFinale = false;
			if ($iView != 0) { 
				if (isset($aData['sEsitoVerifica']) && $aData['sEsitoVerifica']=='KO') { // verifica negativa(KO)
					$bShowSollecito = true;
					if (isset($aData['sEsitoSollecito']) && $aData['sEsitoSollecito']=='KO') { // sollecito negativo(KO)
						$bShowFinale = true;
					}
				}					
			}
			
			$html .= ($sAction == 'edit' ? "<form method='post' id='form-tab-$i' class='buttons' action='transact.php'>" : '');		
			$html .= "<table class='standard' border='".fnTableBorder()."' width='98%'>";

				$html .= "<col width='33%'><col width='33%'><col width='33%'>";
				$html .= "<tbody>";
	
				$html .= "<tr><td colspan='3'>".fnSelectInput('iContratto', 'contratto', $aData, fnSelOptionsFromDb('contratti', "CONCAT(sFornitore,' :: ',sDescrizione)", 'id', " (iTrash=0 AND iArchivio=0 AND iControllo=1) ",'iContratto'), 'index')."</td></tr>";				
				$html .= "<tr><td>".fnTextInput('dtVerifica','data verifica',$aData)."</td><td>".fnSelectInput('iReferenteVerifica', 'referente verifica', $aData, fnSelOptionsFromResources('SIC', '1', 'iReferenteVerifica'), 'index')."</td><td>&nbsp;</td></tr>";				
				$html .= "<tr><td>&nbsp;</td><td>".fnSelectInput('iEsitoVerifica', 'esito verifica', $aData, fnSelOptionsFromResources('ESITO-CONTROLLO', '1', 'iEsitoVerifica'), 'index')."</td><td>&nbsp;</td></tr>";
				$html .= "<tr><td colspan='3'>".fnTextInput('sAppVerificate','apparecchiatura/e',$aData,48)."</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextareaInput('sNoteVerifica','note verifica',$aData)."</td><td>&nbsp;</td></tr>";
				
				$html .= "<tr class='sollecito'".(!$bShowSollecito ? " style='display:none;' " : '')."><td colspan='3'><hr/></td></tr>";
				$html .= "<tr class='sollecito'".(!$bShowSollecito ? " style='display:none;' " : '')."><td>".fnTextInput('dtSollecito','data sollecito',$aData)."</td><td>".fnSelectInput('iReferenteSollecito', 'referente sollecito', $aData, fnSelOptionsFromResources('SIC', '1', 'iReferenteSollecito'), 'index')."</td><td rowspan='3'>";
		
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** INIZIO **
						
						$bEmailButtonShow = true;
						
						if (!empty($iView) && !empty($aData['dtSollecito']) && !empty($aData['iReferenteSollecito']) && !empty($aData['iEsitoSollecito']) && $sAction!='edit') {
							
								
							$sQuery = fnGetResourcesQuery('ESITO-CONTROLLO', array('sMailTo'), 1);
							$rResult = custom_mysql_query($sQuery);
							while ($aRow = mysql_fetch_array ($rResult)) {
								if (!empty($aRow['sMailTo']))
									$aMailTo[$aRow['sMailTo']][] = $aRow['id'];
							}
							
							// BOTTONE INVIO E-MAIL MANAGER-SIC
							if (isset($aData['tsMailMANAGER-SIC']) && $aData['tsMailMANAGER-SIC']=='0000-00-00 00:00:00' /* non è ancora stata spedita la mail a MANAGER-SIC */
									&& isset($aData['iEsitoFinale']) && empty($aData['iEsitoFinale'])
									&& is_allowed('edit', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailMANAGER-SIC'))) {	
								if ($bEmailButtonShow && isset($aMailTo['MANAGER-SIC']) && in_array($aData['iEsitoSollecito'], $aMailTo['MANAGER-SIC'])) {							
									// if (isset($aData['sDecEmail']) && !empty($aData['sDecEmail']) && is_valid_email($aData['sDecEmail'])) {	
										$html .= "<div id='send-email-feedback' class='small-text'></div>";
										$html .= "<br/>";
										$html .= "<span class='send-email buttons small-text'>";
										$html .= "	<button sAction='send-email' sField='tsMailMANAGER-SIC' sEmailTemplate='FORNITORI-KO-MANAGER-SIC' sRecipient='MANAGER-SIC' sCarbonCopy='' iView='$iView' sViewItemLink='$sViewItemLink' id='send-email'>e-mail MANAGER-SIC</button>";
										$html .= "</span>";
										$bEmailButtonShow = false;
									// } else {
									// 	$html .= "<div class='small-text'>inserire e-mail D.E.C. nella scheda del contratto: ";
									// 	$html .= "<a href='./view.php?iView=".$aData['iContratto']."'>".$aData['sFornitore'].", ".$aData['sDescrizione']."</a></div>";
									// }										
								}
							} else if ($aData['tsMailMANAGER-SIC']!='0000-00-00 00:00:00' && is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailMANAGER-SIC'))) {
								$html .= "<span class='small-text'>Email a MANAGER-SIC: ".fnHtmlTimestamp($aData['tsMailMANAGER-SIC'])."</span><br/>";
							}
					
						}
						
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** FINE **
						
				$html .= "</td></tr>";		
	
				$html .= "<tr class='sollecito'".(!$bShowSollecito ? " style='display:none;' " : '')."><td>".fnTextInput('dtRisposta','data risposta',$aData)."</td><td>".fnSelectInput('iEsitoSollecito', 'esito sollecito', $aData, fnSelOptionsFromResources('ESITO-CONTROLLO', '1', 'iEsitoSollecito'), 'index')."</td>";
				$html .= "<tr class='sollecito'".(!$bShowSollecito ? " style='display:none;' " : '')."><td colspan='2'>".fnTextareaInput('sNoteSollecito','note sollecito',$aData)."</td></tr>";
								
				$html .= "<tr class='finale'".(!$bShowFinale ? " style='display:none;' " : '')."><td colspan='3'><hr/></td></tr>";
				$html .= "<tr class='finale'".(!$bShowFinale ? " style='display:none;' " : '')."><td colspan='2'>".fnTextareaInput('sSeguito','seguito',$aData)."</td><td rowspan='2'>";
						
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** INIZIO **
							
						if (!empty($iView) && !empty($aData['dtSollecito']) && !empty($aData['iReferenteSollecito']) && !empty($aData['iEsitoSollecito']) && $sAction!='edit') {

							// BOTTONE INVIO E-MAIL RUP
							if (1 /* è già stata spedita la mail a MANAGER-SIC */
									&& isset($aData['tsMailRUP']) && $aData['tsMailRUP']=='0000-00-00 00:00:00' /* non è ancora stata spedita la mail da MANAGER-SIC a RUP (CC: DEC) */
									&& isset($aData['iEsitoFinale']) && empty($aData['iEsitoFinale'])
									&& is_allowed('edit', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailRUP'))) {
								if ($bEmailButtonShow && isset($aMailTo['MANAGER-SIC']) && in_array($aData['iEsitoSollecito'], $aMailTo['MANAGER-SIC'])) {	
									if (isset($aData['sRupEmail']) && !empty($aData['sRupEmail']) && is_valid_email($aData['sRupEmail'])) {	
										$html .= "<div id='send-email-feedback' class='small-text'></div>";
										$html .= "<br/>";;
										$html .= "<span class='send-email buttons small-text'>";
										$html .= "	<button sAction='send-email' sField='tsMailRUP' sEmailTemplate='FORNITORI-KO-RUP' sRecipient='RUP' sCarbonCopy='DEC' iView='$iView' sViewItemLink='$sViewItemLink' id='send-email'>e-mail RUP</button>";
										$html .= "</span>";
										$bEmailButtonShow = false;
									} else {
										$html .= "<div class='small-text'>inserire e-mail R.U.P. nella scheda del contratto: ";
										$html .= "<a href='./view.php?iView=".$aData['iContratto']."'>".$aData['sFornitore'].", ".$aData['sDescrizione']."</a></div>";
									}	
								}
							} else if($aData['tsMailRUP']!='0000-00-00 00:00:00' && is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailRUP'))) {
								$html .= "<span class='small-text'>Email a RUP: ".fnHtmlTimestamp($aData['tsMailRUP'])."</span><br/>";
							}
						
						}
						
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** FINE **
				
				$html .= "&nbsp;</td></tr>";
				$html .= "<tr class='finale'".(!$bShowFinale ? " style='display:none;' " : '')."><td>".fnSelectInput('iReferenteEsitoFinale', 'referente esito finale', $aData, fnSelOptionsFromResources('SIC', '1', 'iReferenteEsitoFinale'), 'index')."</td><td>".fnSelectInput('iEsitoFinale', 'esito finale', $aData, fnSelOptionsFromResources('ESITO-CONTROLLO', '1', 'iEsitoFinale'), 'index')."</td></tr>";
				
				$html .= "</tbody>";
			$html .= "</table>";

			$html .= "<br/>";
			if ($iView != 0) { 
				$html .= (isset($aData['tsCreatedWhen']) && $aData['tsCreatedWhen']!='0000-00-00 00:00:00' ? "<span class='small-text'>".fnHtmlCreatedWhen($aData['tsCreatedWhen'])."</span><br/>" : '');
				$html .= "<span class='small-text'>".fnHtmlLastModify($aData['tsLastModify'])."</span>";
			}
			
			$html .= "<div>&nbsp;</div>";
			$html .= fnButtons($iView, $i, $iAccessLvl, $aData);
			
		$html .= "</div>";
	
	}
	
	$i = $iTabUploads;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $sViewItemLink, $i))) { 
		
		$html .= "<div id='tabs-$i'>";			
			$html .= fnUploadsTabContent($iView, $sViewItemLink, $i);
		$html .= "</div>";
	
	}
		
	$html .= "</div>";
				
	// contenuto <head> della pagine
	$sHeadContent .= fnViewHeadContent($iTab, $aDisabledTabs);
		
	$bSEARCH_FORM_SHOW = false; // il form di ricerca non viene visualizzato nello script $sViewItemLink
								// non posso assegnare subito questo valore a $bSEARCH_FORM_SHOW perchè
								// alcune funzioni in ../include/view.php utilizzano questa variabile.
								
	require_once('../themes/'.$sTHEME.'/template.php');
	die();




	function fnViewHeadContent ($iTab, $aTabsDisabled) {

		global $sSUITE_NAME, $sAPP;
		
		global $iView, $sViewItemLink;
		
		global $sAction;
		
		global $sJsDocumentReady;
		
		global $aTabsLabels;
		
		$sTabsDisabled = '';
		foreach ($aTabsDisabled as $i) { $sTabsDisabled .= "$i, "; }
		$sTabsDisabled = ($sTabsDisabled!='' ? " disabled: [".substr_replace( $sTabsDisabled, "", -2 )."] " : '');

		$iTabSelected = $i = 0;
		foreach ($aTabsLabels as $sKey => $sValue) {
			if ($sKey == $iTab) { $iTabSelected = $i; }
			$i++;
		}
		
		$sSelectedTab = ($iTabSelected!=0 && !in_array($iTabSelected, $aTabsDisabled) ? ($sTabsDisabled!="" ? ", " : '') . " selected: $iTabSelected " : "");

		$sHeadContent = "

			<script type='text/javascript' charset='utf-8'>
	
				$().ready(function() {
					
					$sJsDocumentReady
					
					$('#tabs').tabs({ $sTabsDisabled $sSelectedTab });
					
					$('button, input:submit', '.buttons').button(); " . ($sAction == 'edit' ? "
							
					$('#iEsitoVerifica').change(function() {
						if ($('#iEsitoVerifica option:selected').text().trim() =='KO') {
							$('.sollecito').show();
						} else {
							/* Sollecito */
							$('#dtSollecito').val('');
							$('#iReferenteSollecito').val('');
							$('#dtRisposta').val('');
							$('#iEsitoSollecito').val('');
							$('#sNoteSollecito').val('');
							$('.sollecito').hide();
							/* Finale */
							$('#sSeguito').val('');
							$('#iEsitoFinale').val('');
							$('.finale').hide();
						}
					});
									
					$('#iEsitoSollecito').change(function() {
						if ($('#iEsitoSollecito option:selected').text().trim() =='KO') {
							$('.finale').show();
						} else {
							/* Finale */
							$('#sSeguito').val('');
							$('#iEsitoFinale').val('');
							$('.finale').hide();
						}
					});
					
					$('#sFornitore').autocomplete({
						source: 'fornitore_suggestions.php',
						minLength: 2,
						select: function(event, ui) {
							$(this).val(ui.item.value);
						}
					}); " : "
					
					$('.send-email button').click(function() {
						$.getJSON('sendmail.php', {	'sAction': $(this).attr('sAction'),
													'sField': $(this).attr('sField'),
													'sEmailTemplate': $(this).attr('sEmailTemplate'),
													'sRecipient': $(this).attr('sRecipient'),
													'sCarbonCopy': $(this).attr('sCarbonCopy'),
													'iView': $(this).attr('iView'),
													'sViewItemLink': $(this).attr('sViewItemLink') },  function (json) {
								if (json[0].sScript=='sendmail.php' && json[0].sError=='') {
									$('#send-email-feedback').html(json[0].sHtml);
									$('.prettydate').prettyDate();
								} else {
									alert(json[0].sError);
								}
						});
						$(this).hide();
					}); 

					") . "
					
				});
				</script> ";
	
		return $sHeadContent;
	}

?>